// option.cs
//
//  Copyright (C) 2008-2010 BOUTRY Arnaud lordphoenix@tuxfamily.org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License  version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Collections;
using System.IO;

namespace swmt{
	public class UndefinedOptionException : Exception{
		private string _optionName;
		public UndefinedOptionException(string optionName): 
		base(String.Format("Undefined application option : {0}",optionName)){
			_optionName = optionName;
		}
		public string OptionName{
			get{return _optionName;}
		}
		
	}
    public class OptionList
{
    private Hashtable _OptionList;
    private string _OptionFile;
    public OptionList()
    {
        _OptionList = new Hashtable();
    }

    public OptionList(string optfile)
    {
        _OptionList = new Hashtable();
        _OptionFile = optfile;
        Load(optfile);
    }

    public bool Load (string optionfile)
    {
		bool result = false;
		if(File.Exists(optionfile)){
		    FileStream fichier = File.Open(optionfile,FileMode.Open);
		    StreamReader read = new StreamReader(fichier);
		    read.BaseStream.Seek(0,SeekOrigin.Begin);
		    string optionstring;
		    while(!read.EndOfStream)
		    {
		        optionstring = read.ReadLine();
		        if (!optionstring.StartsWith("#"))
		            _combine(optionstring);
		    }
		    read.Close();
		    fichier.Close();
			result = true;
		}
		return result;
    }
    public void Save()
    {
        Save(_OptionFile);
    }
    public void Save(string optionfile)
    {
        _OptionFile = optionfile;
        _Save();
    }

    private void _Save()
    {
		FileStream fichier;
		if(!File.Exists(_OptionFile))
			fichier = File.Open(_OptionFile,FileMode.Create);
		else
			fichier = File.Open(_OptionFile,FileMode.Truncate,FileAccess.Write);

		StreamWriter w = new StreamWriter(fichier);
        w.BaseStream.Seek(0,SeekOrigin.Begin);
        foreach(object key in _OptionList.Keys)
        {
            string tmpstring = key.ToString()+"="+_OptionList[key].ToString();
            if(tmpstring!=String.Empty)
                w.WriteLine(tmpstring);
        }
        w.Flush();
        fichier.Flush();
        fichier.Close();
        w.Close();
    }

    private void _combine(string optionstring)
    {
        int cpos=optionstring.IndexOf("=");
        if (cpos!=-1){
            string key = optionstring.Substring(0,cpos);
            string valuekey = optionstring.Substring(cpos+1,optionstring.Length-cpos-1);
            set(key,valuekey);
        }
    }

    public void clear()
    {
        _OptionList.Clear();
    }

    public ArrayList KeyList
    {
        get {
            ArrayList keylist = new ArrayList();
            foreach(object key in _OptionList.Keys)
                keylist.Add(key.ToString());
            return keylist;
        }
    }

    public void set(string name,Object value)
    {
        if(!_OptionList.ContainsKey(name))
            _OptionList.Add(name,value);
        else
            _OptionList[name]=value;
    }

    public bool getBool(string name)
    {
		if(!_OptionList.Contains(name)){
			UndefinedOptionException exc = new UndefinedOptionException(name);
			throw exc;
		}
        return (_OptionList[name].ToString()=="True");
    }
    public string getString(string name)
    {
		if(!_OptionList.Contains(name)){
			UndefinedOptionException exc = new UndefinedOptionException(name);
			throw exc;
		}
        return _OptionList[name].ToString();
    }

    public int getInt(string name)
    {
		if(!_OptionList.Contains(name)){
			UndefinedOptionException exc = new UndefinedOptionException(name);
			throw exc;
		}
        return Int32.Parse(getString(name));
    }

}
}
